package logic

import (
	"context"
	"fmt"
	"zgyzk3/model/mysql"
	"zgyzk3/model/redis"
	"zgyzk3/zgyzk3rpc/internal/svc"
	"zgyzk3/zgyzk3rpc/zgyzk3rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *zgyzk3rpc.LoginRequest) (*zgyzk3rpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Model(&user).Where("tel = ?", in.Tel).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("id有误请重新输入")
	}

	if user.Password != in.Password {
		return nil, fmt.Errorf("密码有误请重新输入")
	}

	get := redis.Client.Get("captcha" + in.Tel)
	if get.Val() != in.Captchacode {
		return nil, fmt.Errorf("图片验证码有误")
	}

	get2 := redis.Client.Get("sendsms" + in.Tel)
	if get2.Val() != in.Sendsmscode {
		return nil, fmt.Errorf("短信验证码有误")
	}

	return &zgyzk3rpc.LoginResponse{Id: int64(user.ID)}, nil
}
